//
//  BkUserSettingController.m
//  bestkeep
//
//  Created by windCould on 16/11/9.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BkUserSettingController.h"
#import "BKMyCommon.h"
#import "BKUserNewAboutBestKeepController.h"
#import "BKUserFeedbackController.h"
#import "BKUserStatusInfo.h"
#import "BKUserPInfoTTACell.h"
//#import "Mediator+BKMy.h"

#define BKUserSettingTableViewCellValue1Id  @"BKUserSettingTableViewCellValue1Id"
#define BKUserSettingTableViewCellDefaultId @"BKUserSettingTableViewCellDefaultId"

@interface BkUserSettingController ()<UIAlertViewDelegate,UIAlertViewDelegate>

@property (nonatomic,strong)NSArray<SectionModel *> * sectionArr;
@property (nonatomic,strong)UIButton * loginBtn; //登录按钮


@end

@implementation BkUserSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(0xf0efef);
    self.navigationItem.title = BKMyLocalizedString(@"BkUserMySetting");
    
    self.sectionArr = [self creatSectionArr];
    [self setUpViews];
}

-(void)setUpViews
{
    UIView * tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 60)];
    tabFootView.backgroundColor = [UIColor clearColor];
    
    UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn = loginBtn;
    loginBtn.frame = CGRectMake(20, 15,SCREENWIDTH-40,45);
    loginBtn.titleLabel.font = UIFontLargest;
    loginBtn.titleLabel.textColor = [UIColor whiteColor];
    loginBtn.layer.cornerRadius = 4;
    loginBtn.layer.masksToBounds = YES;
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"BK_My_退出账户"] forState:UIControlStateNormal];
    [loginBtn setTitle:BKMyLocalizedString(@"BKUserExitLogin") forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(userExitLogin:) forControlEvents:UIControlEventTouchUpInside];
    [tabFootView addSubview:loginBtn];
    self.loginBtn.hidden = ![BKUserStatusInfo currentUser].isLogin;
    self.tableView.tableFooterView = tabFootView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
}


-(NSArray <SectionModel *> *)creatSectionArr
{
    CellModel * hotline = [CellModel cellModelWithTitle:BKMyLocalizedString(@"BKServiceHotline") subTitle:@"400-999-1813" image:nil sel:@"goToCustomerSevercieHotline"];
    hotline.reuseIdentifier = BKUserSettingTableViewCellValue1Id;
    hotline.style = UITableViewCellStyleValue1;
    hotline.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    CellModel * FeedBack = [CellModel cellModelWithTitle:BKMyLocalizedString(@"BKUserFeedback") subTitle:nil image:nil sel:@"goToFeedback"];
    FeedBack.reuseIdentifier = BKUserSettingTableViewCellDefaultId;
    FeedBack.style = UITableViewCellStyleDefault;
    FeedBack.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    CellModel * score = [CellModel cellModelWithTitle:BKMyLocalizedString(@"BKUserScore") subTitle:nil image:nil sel:@"goToScore"];
    score.reuseIdentifier = BKUserSettingTableViewCellDefaultId;
    score.style = UITableViewCellStyleDefault;
    score.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    CellModel * about = [CellModel cellModelWithTitle:BKMyLocalizedString(@"BKAboutBESTKEEP") subTitle:nil image:nil sel:@"goToAboutBestKeep"];
    about.reuseIdentifier = BKUserSettingTableViewCellDefaultId;
    about.style = UITableViewCellStyleDefault;
    about.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    SectionModel * sect = [SectionModel sectionModelWithTitle:@"" cells:@[hotline,FeedBack,score,about]];
    return @[sect];
}


#pragma mark ================ cell的点击事件 =========================

-(void)goToCustomerSevercieHotline{

    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认拨打400-999-1813" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.tag = 991;
    [alertView show];
    
}

//执行alertView的代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
        if (alertView.tag == 991) {
            
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", @"400-999-1813"]];
            [[UIApplication sharedApplication] openURL:url];
        }
        
        if (alertView.tag == 992) {
            
            [BKUserStatusInfo clearHistoryData];
            [self.navigationController popViewControllerAnimated:YES];
            
//            BKUserStatusInfo * userInf = [BKUserStatusInfo currentUser];
//           
//            [userInf.realm beginWriteTransaction];
//            userInf.isLogin = NO;
//            userInf.TGT = @"";
//            userInf.ServiceST = @"UTOUU-ST-INVALID";
//            userInf.LoginST = @"UTOUU-ST-INVALID";
//            userInf.userId = 0;
//            userInf.job = @"";
//            userInf.email = @"";
//            userInf.realAuthStatus = @"";
//            userInf.levelNum = @"";
//            userInf.vipFlag = @"";
//            userInf.photo = @"";
//            userInf.userPwd = @"";
//            userInf.stockAccountStateName = @"";
//            userInf.realAuth = NO;
//            userInf.visitorCode = @"";
//            userInf.money = @"";
//            userInf.payBind = @"";
//            userInf.nickName = @"";
//            userInf.level = @"";
//            userInf.roles = @"";
//            userInf.levelNum = @"";
//            userInf.realAuthStatus = @"";
//            userInf.QQbinding = @"";
//            userInf.wechatBindin = @"";
//            userInf.unreadMessageNum = @"";
//            userInf.deveiceToken = @"";
//            [userInf.realm commitWriteTransaction];
            
            
//            [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES];
//            [[NSNotificationCenter defaultCenter]postNotificationName:UserDidLogoutNotification object:nil];
        }
    
    }

}

-(void)alertViewCancel:(UIAlertView *)alertView
{
    if (alertView.tag == 992) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }

}


-(void)goToFeedback{
   
    BOOL  isLogin = [BKMyCommon userIsLoginWithInController:self];
    if (isLogin) {
        
        BKUserFeedbackController * feedBackVC = [[BKUserFeedbackController alloc]init];
        [self.navigationController pushViewController:feedBackVC animated:YES];
    }
}

-(void)goToScore{

      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%d&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8", 1035553463]]];
}

-(void)goToAboutBestKeep{

    BKUserNewAboutBestKeepController * aboutUsVC = [[BKUserNewAboutBestKeepController alloc]init];
    [self.navigationController pushViewController:aboutUsVC animated:YES];
}

//退出登录
-(void)userExitLogin:(UIButton *)btn
{
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定要退出登录吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag = 992;
    [alert show];
    
}

-(BOOL)isLogin
{
    BKUserStatusInfo * userStatusInfo = [BKUserStatusInfo currentUser];
    return userStatusInfo.isLogin;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    SectionModel * sect = _sectionArr[section];
    return sect.cells.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
         return 10.0f;
    }
    return 0.00001f;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    SectionModel * sect = _sectionArr[indexPath.section];
    CellModel * item = sect.cells[indexPath.row];
    
    BKUserPInfoTTACell * cell = [tableView dequeueReusableCellWithIdentifier:BKUserPInfoTTACellIdentify];
    if (!cell) {
        
        cell = [[BKUserPInfoTTACell alloc]initWithStyle:item.style reuseIdentifier:BKUserPInfoTTACellIdentify];
    }
    
    cell.headTitleStr = item.title;
    cell.subTitleStr = item.subTitle;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
   /*
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:item.reuseIdentifier];
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:item.style reuseIdentifier:item.reuseIdentifier];
    }
    cell.textLabel.text = item.title;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = RGBCOLOR(51, 51, 51, 1);
    cell.detailTextLabel.text = item.subTitle;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.textColor = RGBCOLOR(153, 153, 153, 1);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = item.accessoryType;
    cell.separatorInset = UIEdgeInsetsMake(0, -100, 0, 0);
    return cell;
   */
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    SectionModel * sect = _sectionArr[indexPath.section];
    CellModel * item = sect.cells[indexPath.row];
    
    if (item.selectorString) {
        
        SEL  selector = NSSelectorFromString(item.selectorString);
        if ([self respondsToSelector:selector]) {
   
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
            
            [self performSelector:selector];
        }
        
    }
    
}




@end
